import Vue from 'vue';
import Vuex from 'vuex';
import user from './user'
import org from './org'

Vue.use(Vuex);

const store = new Vuex.Store({
  modules: {
    user: user,
    org: org
  },
  state: {
    count: 0,
    todos: [
      { id: 1, text: 'todo1', done: true },
      { id: 2, text: 'todo2', done: false }
    ],
    code: '',
    accessToken: ''
  },
  mutations: {
    increment(state) {
      state.count++
    }
  },
  getters: {
    doneTodos: state => {
      return state.todos.filter(todo => todo.done)
    },
    getTodoById: state => (id) => {
      return state.todos.find(todo => todo.id === id);
    },
    getCode: state => () => {
      return state.code;
    },
    setCode: state => (code) => {
      state.code = code;
    },
    getAccessToken: state => () =>{
      return state.accessToken;
    },
    setAccessToken: state => (accessToken) => {
      state.accessToken = accessToken;
    }
  },
  actions: {
    increment({ commit }) {
      commit('increment')
    },
    incrementAsync({ commit }) {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          commit('increment')
        }, 1000);
        resolve("success");
      })
    }
  }
});
export default store;
